import {
  Component,
  Input,
  OnInit,
} from '@angular/core';
import { ApiService } from '../../core/services/http/api.service';
import PinyinMatch from 'pinyin-match';
import { UploadFile } from '@shared/apes/upload';
import { ApesModalRef } from '@shared/apes/modal';
// import { deepClone } from '../../utils/utils';

@Component({
  selector: 'apes-dialog-cascader',
  templateUrl: './apes-dialog-cascader.component.html',
})
export class ApesDialogCascaderComponent implements OnInit {
  prefixCls = 'apes-dialog-cascader';

  rowData: any[];
  resultData: any = '';
  @Input() id = '';
  @Input() title = '查询标准配件';

  @Input() valueFormatter: any;


  constructor(
    private apiService: ApiService,
    public modal: ApesModalRef,
  ) {
  }

  ngOnInit() {
    this.queryData(['M01'], 0);
  }

  destroyModal(): void {
    this.modal.destroy(this.resultData);
  }

  data: any[] = [];
  listData: any[] = [];
  _fileList: any[] = [];

  private queryData(value, levels) {
    let condition = [
      // ['M0101','M0103']
      { 'field': 'parents.id', 'operator': 'IN', 'value': value },
      // {'field':'classify.id','operator':'EQ','value':value}
    ];
    let data = { condition: condition, paged: false };
    this.apiService.post(this.id, { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          this.showList(body, levels);
        }
      });

  }

  pinyin = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

  showList(data, levels) {
    this.listData[levels] = { data: [] };
    this.pinyin.map(py => {
      let show = {
        en: '',
        data: [],
      };
      data.map(d => {
        let sh = null;
        sh = PinyinMatch.match(d['name'], py);
        if (sh != null && sh[0] == 0) {
          if (!d['save']) {
            d['save'] = true;
            show['en'] = py;
            show['data'].push(d);
          }
        }
      });
      if (show.en != '') {
        this.listData[levels].data.push(show);
      }
    });
  }

  getLowerData(list, data, levels) {
    // console.log(list);
    let value = data.id;
    let length = this.listData.length;
    this.listData.splice(levels, length);
    list.map(l => {
      l['data'].forEach(res => {
        res['checked'] = false;
      });
    });
    data['checked'] = true;
    if (data.finality) {
      this.resultData = [data];
    } else {
      if (data.lastStage) {
        this.queryLastStageData(value, levels);
      } else {
        this.queryData(value, levels);
      }
      this.resultData = [];
    }
  }

  queryLastStageData(value, levels) {
    let condition = [
      { 'field': 'classify.id', 'operator': 'EQ', 'value': value },
      { 'field': 'matchCProduct', 'operator': 'EQ', 'value': true },
    ];
    let data = { condition: condition, paged: false };
    let api = 'cm.category.findAll';
    this.apiService.post(api, { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          console.log(body);
          body.map(b => {
            b['finality'] = true;
          });
          this.showList(body, levels);
        }
      });
  }


  showPhoto(data) {
    this._fileList = data['categoryPhotos'] ? JSON.parse(data['categoryPhotos']) : [];
    // return data['categoryPhotos'] ? JSON.parse(data['categoryPhotos']) : [];
  }

  get fileList() {
    return this._fileList || [];
  }

  handleRemove = (file: UploadFile) => {
    return false;
  };

  handleShowList = () => {
    let showList = {
      showPreviewIcon: true,
      showRemoveIcon: false,
      showDownloadIcon: true,
    };
    return showList;
  };

  selectData(data){
    this.resultData = [data];
    this.destroyModal();
  }

  sortShowData(data){
    data.sort(
      function compareFunction(param1, param2) {
        return param1.name.localeCompare(param2.name,"zh");
      }
    );
    return data;
  }
}

